package com.ssy.lingxi.report.serviceimpl;

import cn.hutool.core.collection.CollectionUtil;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.report.entity.report.DataLayoutDO;
import com.ssy.lingxi.report.model.vo.request.UpdateDataLayoutVO;
import com.ssy.lingxi.report.model.vo.response.DataLayoutVO;
import com.ssy.lingxi.report.repository.report.DataLayoutRepository;
import com.ssy.lingxi.report.service.IDataLayoutService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 数据布局接口实现
 * @author 伍将
 * @date 2020/11/19
 * @version 2.0.0
 */
@Service
public class DataLayoutServiceImpl implements IDataLayoutService {

    @Resource
    private DataLayoutRepository repository;

    /**
     * 获取数据布局
     * @param sysUser 登录用户
     * @return 数据布局
     */
    @Override
    public Wrapper<List<DataLayoutVO>> getDetail(UserLoginCacheDTO sysUser) {
        List<DataLayoutVO> tempList =null;
        List<DataLayoutDO> doList = repository.findByMemberIdAndRoleIdAndUserId(sysUser.getMemberId(), sysUser.getMemberRoleId(),sysUser.getUserId());
        if (doList != null && doList.size() > 0) {
            tempList=doList.stream().map(o ->{
                DataLayoutVO entity = new DataLayoutVO();
                BeanUtils.copyProperties(o,entity);
                return entity;
            }).sorted(Comparator.comparing(DataLayoutVO::getSort)).collect(Collectors.toList());
        }
        return Wrapper.success(tempList);
    }

    /**
     * 更新数据布局
     * @param sysUser 登录用户
     * @param layoutVOS 布局数据
     * @return 更新结果
     */
    @Transactional
    @Override
    public Wrapper<Void> update(UserLoginCacheDTO sysUser, List<UpdateDataLayoutVO> layoutVOS) {
        //清空所有
        if(CollectionUtil.isEmpty(layoutVOS)){
            // 删除旧数据
            repository.deleteByMemberIdAndRoleId(sysUser.getMemberId(), sysUser.getMemberRoleId());
            return Wrapper.success();
        }
        //删除取消显示的选项
        List<DataLayoutDO> oldDataLayoutList = repository.findByMemberIdAndRoleIdAndUserId(sysUser.getMemberId(), sysUser.getMemberRoleId(), sysUser.getUserId());
        if(CollectionUtil.isNotEmpty(oldDataLayoutList)){
            //修改项id
            List<UpdateDataLayoutVO> reqIdsStream = layoutVOS.stream().filter(o -> o.getId() != null).collect(Collectors.toList());
            if(CollectionUtil.isNotEmpty(reqIdsStream)) {
                Map<Long, UpdateDataLayoutVO> reqIdsMap = reqIdsStream.stream().collect(Collectors.toMap(UpdateDataLayoutVO::getId, e -> e));
                //取消显示的选项id
                List<Long> delIds = oldDataLayoutList.stream().map(DataLayoutDO::getId).filter(id -> reqIdsMap.get(id) == null).collect(Collectors.toList());
                if(CollectionUtil.isNotEmpty(delIds)){
                    repository.deleteByIdInAndUserId(delIds,sysUser.getUserId());
                }
            }
        }
        List<DataLayoutDO> resultList = layoutVOS.stream().map(r -> {
            DataLayoutDO entity = new DataLayoutDO();
            entity.setId(r.getId());
            entity.setName(r.getName());
            entity.setSort(r.getSort());
            entity.setIsShow(r.getIsShow());
            entity.setMemberId(sysUser.getMemberId());
            entity.setRoleId(sysUser.getMemberRoleId());
            entity.setUserId(sysUser.getUserId());
            return entity;
        }).collect(Collectors.toList());
        repository.saveAll(resultList);
        return Wrapper.success();
    }
}
